home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: Problem with generic classes/templates
- Date: 18 Jan 1996 21:48:38 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Distribution: world
- Message-ID: <4dmf7m$b6a@colossus.holonet.net>
- References: <4dlerb$r3r@finch.doc.ic.ac.uk>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Benjamin Jefferys (brj@doc.ic.ac.uk) wrote:
- [...]
-
- : class meringue{
- [OK]
- : };
-
- : template <class X> class marzipan<X>{
- ^^^ OMIT THIS
- : X ian;
- : public:
- : marzipan<X>(/* what goes here? */) { /* and what goes here? */ }
- ^^^ OMIT THIS
- : }
-
- Well, it really depends on how you want to construct marzipan<X>
- objects. I think here you need to concern yourself more with
- marzipans, and leave any decision about X's up to the caller by
- letting him pass in an X as follows:
- marzipan(const X& x) : ian(x) {} // inits ian with X's copy ctor
- and you might also want a default ctor which could simply be:
- marzipan() {} // inits ian with X's default ctor
-
- : Once I've got these, I try a:
-
- : marzipan<meringue> bob;
-
- This would call the default ctor, and bob.ian would be a default
- meringue. Alternatively, you could construct a meringue object
- as you like, and then invoke the marzipan(const X&) ctor, e.g.
-
- meringue m( whatever );
- marzipan<meringue> bob( m ); // bob.ian is a copy of m
-
- This is the only approach that makes much sense to me. You could
- of course make additional marzipan ctors, that take other arguments
- which you pass on to the X ctor, but that would lose the generality
- of your template. For example, you could have
- marzipan( float a, float b ) : X(a,b) {}
- but this would fail to compile for many X's that are not meringues.
-
- [...]
-
- : (As a matter of interest, the generic class(es) implements a doubly-linked
- : list, which obviously has to store any class to be useful)
-
- Hmm, linked lists are usually done with pointers, not contained
- classes. I'm not sure where your marzipan template fits into your
- design, but I sense trouble!
- --
- Russell Blackadar, russell@mdli.com
-